package pathways.util;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import pathways.Database.Implementation.CDatabase;
import pathways.SpeciesSpace.CField;
import pathways.util.Debug.CDebug;
import pathways.util.helper.CItem;

public class CDBUtil
{
  // public constants with a database orientation
  //        that is, used in the database somehow
  
  public static final int COORDINATE_TYPE_RECTANGULAR = 1; // used in tblPathway
  
  // private vars
  private static Vector tissues = null;
  private static Vector organs = null;
  private static Vector cells = null;
  private static Vector subCellSubs = null;
  private static Vector subCellProds = null;
  private static Vector fields = null;
  
  public CDBUtil()
  {
    
  }
  
  public static Vector getDataFields(CDatabase db)
  {
    if (fields == null)
    {
      ResultSet rs = db.getSelect().getDataFields();
      CField myField = null;
      fields = new Vector();
      
      try {
        while(rs.next())
        {
          myField = new CField();
          myField.setID(rs.getInt(1));
          myField.setName(rs.getString(2));
          myField.setUnit(rs.getString(3));
            
          fields.add(myField);
        }   
      } catch (SQLException sqle) {
         CDebug.println("pathways.util.CDBUtil", "getDataFields() Exception: " + sqle.toString());
         fields = null;
      }  
    }
    
    return fields;
  }
  
  public static Vector getTissues(CDatabase db)
  {
    if (tissues == null)
    {
      ResultSet rs = db.getSelect().getTissues();
      
      try {
          tissues = convertRSToVector(rs);
      } catch (SQLException sqle) {
        CDebug.println("pathways.util.CDBUtil","getTissues() Exception: " + sqle.toString()); 
      }
    }
    
    return tissues;
  }
  
  public static Vector getOrgans(CDatabase db)
  {
    if (organs == null)
    {
      ResultSet rs = db.getSelect().getOrgans();
      
      try {
          organs = convertRSToVector(rs);
      } catch (SQLException sqle) {
        CDebug.println("pathways.util.CDBUtil","getOrgans() Exception: " + sqle.toString());
      }
    }
    
    return organs;
  }
  
  public static Vector getCells(CDatabase db)
  {
    if (cells == null)
    {
      ResultSet rs = db.getSelect().getCells();
      
      try {
          cells = convertRSToVector(rs);
      } catch (SQLException sqle) {
        CDebug.println("pathways.util.CDBUtil","getCells() Exception: " + sqle.toString());
      }
    }
    
    return cells;
  }
  
  public static Vector getSubCellSubs(CDatabase db)
  {
    if (subCellSubs == null)
    {
      ResultSet rs = db.getSelect().getSubCellSubs();
      
      try {
          subCellSubs = convertRSToVector(rs);
      } catch (SQLException sqle) {
        CDebug.println("pathways.util.CDBUtil","getSubCellSubs() Exception: " + sqle.toString());
      }
    }
    
    return subCellSubs;
  }
  
  public static Vector getSubCellProds(CDatabase db)
  {
    if (subCellProds == null)
    {
      ResultSet rs = db.getSelect().getSubCellProds();
      
      try {
          subCellProds = convertRSToVector(rs);
      } catch (SQLException sqle) {
        CDebug.println("pathways.util.CDBUtil","getSubCellProds() Exception: " + sqle.toString());
      }
    }
    
    return subCellProds;
  }
  
  private static Vector convertRSToVector(ResultSet rs) throws SQLException
  {
    Vector v = new Vector();
    CItem item = null;
    
    while(rs.next())
    {
      item = new CItem(rs.getInt(1),rs.getString(2));
      
      v.add(item);   
    }
    
    return v;
  }
  
  public static String getSortVersion(String cmpdName)
  {
    /*
    Compound name is something like
    5.Gluco.6.Genesis.
    then the sort version is
    GlucoGenesis where everything between two dots
    is taken for sorting and the rest is discarded
    
    if nothing is enclosed in dots then the whole thing
    is returned as it is
    */
    
    String sortVersion = "";
    
    int startIndex = 0;
    int index1 = 0;
    int index2 = 0;
    
    while (index1 >= 0 && index2 >= 0)
    {
      index1 = cmpdName.indexOf('.',startIndex);
      startIndex = index1 + 1;
      index2 = cmpdName.indexOf('.',startIndex);
      
      //System.out.println(index1);
      //System.out.println(index2);
      
      if (index1 >= 0 && index2 >= 0)
      {
	index1++;
	sortVersion = sortVersion + cmpdName.substring(index1,index2);
	startIndex = index2 + 1;  
      }
    }
    
    // if nothing in dots then just return the original
    if (sortVersion.equals(""))
      sortVersion = cmpdName;
    
    return sortVersion;     
  }
}